package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);

        Page<Member> members = memberDao.queryPage(queryString);
        for (Member member : members) {
            Date birthday = member.getBirthday();
            if (birthday != null) {
                Date now = new Date();
                int i = now.getYear() - birthday.getYear();
                member.setAge(i);
            }
        }
        return new PageResult(members.getTotal(), members.getResult());
    }

    public void delete(Integer id) {
        //查看中间关联关系
        if (memberDao.findRelevanceById(id) == 0) {
            memberDao.deleteById(id);
        }
    }

    //编辑会员
    public void edit(Member member) {
        Date birthday = member.getBirthday();
        if (birthday != null) {
            Date now = new Date();
            int i = now.getYear() - birthday.getYear();
            member.setAge(i);
        }
        memberDao.edit(member);
    }

    //通过id查找
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //查找健康管理师
    public List<Map<String, Object>> findManagerByRoleId() {
        List<String> nameList = memberDao.findManagerByRoleId();
        List<Map<String, Object>> list = new ArrayList<>();
        for (String s : nameList) {
            Map<String, Object> map = new HashMap<>();
            map.put("value", s);
            list.add(map);
        }
        return list;
    }
}
